﻿
Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_ToDo_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")

                LoadListHour(ddlbeHour)
                LoadListHour(ddlDueDateHour)
                LoadListHour(ddlRemindDateHour)

                '  ddleHour.SelectedIndex = 23
                LoadListMinutes(ddlbeMinutes)
                LoadListMinutes(ddlDueDateMinute)
                LoadListMinutes(ddlRemindDateMinute)


                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim _UserId As Integer = Session("UserId")
            Dim p As ToDoList = (From q In data.ToDoLists Where q.Id = hidID.Value And q.UserId = _UserId Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtSubject.Text = IIf(IsNothing(p.Subject) = True, "", p.Subject)
                txtDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)

                Dim dtStartdate As Date = CType(p.StartDate, Date)
                txtStartdate.Text = dtStartdate.ToString("dd/MM/yyyy")
                ddlbeHour.SelectedValue = dtStartdate.Hour()
                ddlbeMinutes.SelectedValue = dtStartdate.Minute

                Dim dtDuedate As Date = CType(p.DueDate, Date)
                If dtDuedate <> New Date(1900, 1, 1) Then
                    txtDuedate.Text = dtDuedate.ToString("dd/MM/yyyy")
                    ddlDueDateHour.SelectedValue = dtDuedate.Hour()
                    ddlDueDateMinute.SelectedValue = dtDuedate.Minute
                End If

                Dim dtReminddate As Date = CType(p.RemindDate, Date)
                If dtReminddate <> New Date(1900, 1, 1) Then
                    txtReminddate.Text = dtReminddate.ToString("dd/MM/yyyy")
                    ddlRemindDateHour.SelectedValue = dtReminddate.Hour()
                    ddlRemindDateMinute.SelectedValue = dtReminddate.Minute
                End If

                chkIsActive.Checked = p.IsActive


                'txtNumberremind.Text = IIf(IsNothing(p.NumberRemind) = True, "", p.NumberRemind)
                'txtStatus.Text = IIf(IsNothing(p.Status) = True, "", p.Status)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As ToDoList = (From q In data.ToDoLists Where q.Id = hidID.Value).SingleOrDefault
            Try

                p.Subject = txtSubject.Text.Trim()
                p.Description = txtDescription.Text.Trim()
                p.StartDate = StringToDateHour(txtStartdate.Text, ddlbeHour.SelectedValue, ddlbeMinutes.SelectedValue)
                p.DueDate = StringToDateHour(txtDuedate.Text.Trim(), ddlDueDateHour.SelectedValue, ddlDueDateMinute.SelectedValue)
                p.RemindDate = StringToDateHour(txtReminddate.Text.Trim(), ddlRemindDateHour.SelectedValue, ddlRemindDateMinute.SelectedValue)
                p.IsActive = chkIsActive.Checked
                data.SaveChanges()
                'Insert_App_Log("Insert Todolist:" & txtTitle.Text.Trim & "", Function_Name.Todolist, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Nhập dữ liệu thành công.'); window.location.href='" & "Detail.aspx?Id=" & hidID.Value & "'", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub


    Sub LoadListHour(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 23
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    Sub LoadListMinutes(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 59
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub

#End Region
End Class
